﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HYQuick
{
    internal class HYQuickSafePluginRef:IHYQuickPlugin
    {
        public HYQuickSafePluginRef(IHYQuickPlugin plugin)
        {
            m_plugin = plugin;
        }

        public IHYQuickPluginMethod[] GetMethods()
        {
            try
            {
                return m_plugin.GetMethods();
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin", "plugin load error", "invoking:GetMethods() and caused" + ex.Message + Environment.NewLine + ex.StackTrace);//仍然存疑
                return null;
            }
        }
        private IHYQuickPlugin m_plugin;
    }

    internal class HYQuickSafePluginMethodRef:IHYQuickPluginMethod
    {
        public HYQuickSafePluginMethodRef(IHYQuickPluginMethod method)
        {
            m_method = method;    
        }

        public string GetName()
        {
            try
            {
                return m_method.GetName();
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", "plugin GetName() error", "invoking: GetName() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
                return null;
            }
        }

        public string GetDescription(IHYQuickWindow quickWindow)
        {
            try
            {
                return m_method.GetDescription(quickWindow);
            }
            catch (Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: GetName() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
                return null;
            }
        }

        public string AvailableApplicationName()
        {
            try
            {
                return m_method.AvailableApplicationName();
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: AvailableApplicationName() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
                return null;
            }
        }

        public void Invoke(object sender,IHYQuickWindow quickWindow)
        {
            try
            {
                m_method.Invoke(sender, quickWindow);
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: Invoke() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }

        public bool AcceptArgs()
        {
            try
            {
                return m_method.AcceptArgs();
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: AcceptArgs() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
                return false;
            }
        }

        public bool GetIcon(IHYQuickWindow quickWindow,out System.Windows.Media.ImageSource icon)//表示一个具有高度、宽度的对象类型
        {
            try
            {
                return m_method.GetIcon(quickWindow, out icon);
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: GetIcon() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
                icon = null;
                return false;
            }
        }

        public void KeyDown(IHYQuickWindow quickWindow,System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                m_method.KeyDown(quickWindow, e);
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: KeyDown() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }

        public void TextChanged(IHYQuickWindow quickWindow,System.Windows.Controls.TextChangedEventArgs e)//为事件提供数据
        {
            try
            {
                m_method.TextChanged(quickWindow, e);
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: TextChanged() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }

        public void Closed(IHYQuickWindow quickWindow)
        {
            try
            {
                m_method.Closed(quickWindow);
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: Closed() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }

        public void PageDown(IHYQuickWindow quickWindow)
        {
            try
            {
                m_method.PageDown(quickWindow);
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: PageDown() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }   

        public void PageUp(IHYQuickWindow quickWindow)
        {
            try
            {
                m_method.PageUp(quickWindow);
            }
            catch(Exception ex)
            {
                HYQuickVitality.UpdateVitality("error in plugin method", m_method.GetName(), "invoking: PageUp() and caused " + ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }

        private IHYQuickPluginMethod m_method;
    }
}
